<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\Component;

/*
  Table structure
  _________________________________________
  CREATE TABLE `lg_menu` (
  `menu_id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) DEFAULT '0',
  `sort_order` int(11) DEFAULT '0',
  PRIMARY KEY (`menu_id`),
  KEY `parent_id` (`parent_id`)
  ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

  CREATE TABLE `lg_menu_language` (
  `menu_id` int(11) NOT NULL AUTO_INCREMENT,
  `lang_id` int(11) NOT NULL DEFAULT '0',
  `title` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`menu_id`,`lang_id`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 */

use Lg\Database\Model;

/**
 * Package Lg\Component  
 * 
 * Class SimpleCategories
 *
 * @author allen <allen@w4u.cn>
 */
class SimpleCategories extends Model {

    public $primary_table;
    public $language_table;
    public $primarykey;

    public function __construct($primary_table = 'category', $language_table = 'category_language', $primarykey = 'category_id') {
        $this->primary_table = $primary_table;
        $this->language_table = $language_table;
        $this->primarykey = $primarykey;
    }

    public function getCategories($filters = []) {
        $parent = isset($filters['parent']) ? $filters['parent'] : 0;
    }

    public function getCategoryById($id) {
        $primary_table = $this->db()->tableName($this->primary_table);
        $language_table = $this->db()->tableName($this->language_table);
        $sql = "select * from {$primary_table} as p "
                . "left join {$language_table} as l on p.{$this->primarykey}=l.{$this->primarykey} "
                . "WHERE p.{$this->primarykey}={$id}";
        return $this->db()->getRow($sql);
    }

    public function deleteCategory($id) {
        $this->db()->delete($this->primary_table, [$this->primarykey => $id]);
        $this->db()->delete($this->language_table, [$this->primarykey => $id]);
    }

    public function buildTree() {
        
    }

    public function buildMenu() {
        $primary_table = $this->db()->tableName($this->primary_table);
        $language_table = $this->db()->tableName($this->language_table);
        $sql = "select * from  {$primary_table} p "
                . "LEFT JOIN {$language_table} l ON p.{$this->primarykey}=l.{$this->primarykey} "
                . "WHERE p.status=1 "
                . "order by p.sort_order asc";
        $query = $this->db()->getAll($sql);
        $refs = array();
        $list = array();
        foreach ($query as $row) {
            $ref = & $refs[$row->{$this->primarykey}];

            $ref['parent_id'] = $row->parent_id;
            $ref['title'] = $row->title;

            if ($row->parent_id == 0) {
                $list[$row->{$this->primarykey}] = & $ref;
            } else {
                $refs[$row->parent_id]['children'][$row->{$this->primarykey}] = & $ref;
            }
        }
        return $list;
    }

    function toUL(array $array) {
        static $level = 0;
        $html = '<ul class="'.  value_condition($level,'submenu','menu').'">' . PHP_EOL;

        foreach ($array as $value) {
            $html .= '<li>';
            $html .= '<a href="#">' . $value['title'] . '</a>';
            if (!empty($value['children'])) {
                $level++;
                $html .= $this->toUL($value['children']);
            }
            $html .= '</li>' . PHP_EOL;
        }

        $html .= '</ul>' . PHP_EOL;

        return $html;
    }

}
